package com.epfly.promo.activity.impl.discount.predicate;

import com.epfly.iface.promo.activity.enums.ActivityTypeEnum;
import com.epfly.iface.promo.activity.enums.IsActiveEnum;
import com.epfly.promo.activity.entity.ActivityBoundEntity;
import com.epfly.promo.activity.entity.ActivityEntity;
import com.epfly.promo.activity.entity.UserActivityStatisticsEntity;
import com.epfly.promo.activity.feign.DataRestaurantFeignService;
import com.epfly.promo.activity.feign.UserFeignService;
import com.epfly.promo.activity.impl.discount.ActivityWrapper;
import com.ifood.model.merchants.param.response.IdentityEmpResp;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Objects;

@Service
public class BoundPredicateHelper {

    @Resource
    private DataRestaurantFeignService dataRestaurantFeignService;

    @Resource
    private UserFeignService userFeignService;


    public boolean assertBasic(ActivityWrapper activityWrapper) {
        ActivityEntity activityEntity = activityWrapper.getActivityEntity();

        // 非订单优惠(即：1.满减;2.折扣;)，不受限额限次约束
        if (!activityEntity.getActivityType().equals(ActivityTypeEnum.ORDER.getKey())) {
            return true;
        }

        String userID = activityWrapper.getUserID();

        Boolean isMember = userFeignService.isMember(userID).getIsMember();
        IdentityEmpResp identity = dataRestaurantFeignService.getIdentity(userID);
        Boolean isEmployee = identity.getIsEmployee();

        // 既非会员，亦非员工，即是散户
        boolean isConsumer = !isMember && !isEmployee;

        // 散户不考虑限额限次
        return isConsumer;
    }


    /**
     * XX表示日或餐别
     * 判断限额限次实体自身属性
     */
    protected boolean assertBoundBasic(ActivityBoundEntity boundEntity) {

        // 没有设置XX限额限次
        if (Objects.isNull(boundEntity)) {
            return true;
        }

        // XX限额限次处于停用状态
        if (boundEntity.getIsActive().equals(IsActiveEnum.NO.getKey())) {
            return true;
        }

        // XX限额未开启，XX限次未开启
        return boundEntity.getAmountBound().compareTo(BigDecimal.ZERO) == 0 && boundEntity.getCountBound() == 0;
    }

    /**
     * XX表示日或餐别
     * 基于已统计数据判断限额限次配置规则
     */
    protected boolean assertBoundRule(UserActivityStatisticsEntity statistics, ActivityBoundEntity boundEntity) {
        // 用户仍未享受过此优惠
        if (Objects.isNull(statistics)) {
            return true;
        }

        // XX限额开启，XX限次未开启，并且"XX累计金额"小于"XX限额"
        if (boundEntity.getAmountBound().compareTo(BigDecimal.ZERO) > 0 && statistics.getTotalAmount().compareTo(boundEntity.getAmountBound()) < 0
                && boundEntity.getCountBound() == 0) {
            return true;
        }

        // XX限次开启，XX限额未开启，并且"XX累计次数"小于"XX限次"
        if (boundEntity.getCountBound() > 0 && statistics.getTotalCount() < boundEntity.getCountBound()
                && boundEntity.getAmountBound().compareTo(BigDecimal.ZERO) == 0) {
            return true;
        }

        // XX限额开户，XX限次开启，并且"XX累计金额"小于"XX限额"，并且"XX累计次数"小于"XX限次"
        return boundEntity.getAmountBound().compareTo(BigDecimal.ZERO) > 0 && boundEntity.getCountBound() > 0
                && statistics.getTotalAmount().compareTo(boundEntity.getAmountBound()) < 0 && statistics.getTotalCount() < boundEntity.getCountBound();

    }
}
